import { JXDef } from "../../../conventions/JXCommon";
import GViewBase from "../../../Core/GView/GViewBase";
import { AudioMgr } from "../../../Core/Manager/AudioMgr";
import TyqEventMgr from "../../../tyq/tyq-event-mgr";
import { VIEW_ID } from "../../Common/UI";
import RJXItem from "../../Data/RunTime/RunRaw/RJXItem";
import GameMgr from "../../Logic/GameMgr";
import JXItem from "../Common/JXItem";
import { JXEOverAniNames } from "../JXBattle/JXULDefine";
import { GCtrl } from './../../../Core/GCtrl';
import { CMsg, ITEM_DETAIL_FLAG, PART_BTL_SCENE, PART_ITEM } from './../../Common/Define';
import { Res } from './../../Common/UIResources';
import { JXBattleAnalysis } from './../JXBattle/JXBattleAnalysis';
import BtHeroHurt from "./BtHeroHurt";
import JXResultHero from "./JXResultHero";

const { ccclass, property, menu } = cc._decorator;
@ccclass @menu("View/Fight/BattleResultCtrl")
export default class BattleResultCtrl extends GViewBase {
	@property(cc.Node) stars: cc.Node = null;
	@property(cc.Node) rewardLayer: cc.Node = null;
	@property(cc.Node) heroLayer: cc.Node = null;
	@property(cc.Node) btnLayer: cc.Node = null;
	@property(cc.Node) shareBtn: cc.Node = null;
	@property({ type: sp.Skeleton, tooltip: "胜利失败特效" }) sk_Over: sp.Skeleton = null;
	@property({ type: cc.Node, tooltip: "英雄伤害表" }) heroHurt: cc.Node = null;
	@property({ type: cc.Node, tooltip: "去增强" }) quzengqiang: cc.Node = null;


	private _ans: JXBattleAnalysis = null;
	private _cb: Function = null;
	private _isWin: boolean = null;
	private _level: number = null;
	private _levelRaw: SStoryDataRaw | STowerDataRaw = null;
	private _TowerData: STowerDataRaw = null
	private _secenId: number = null;
	private _star: number = 0;
	private _reward: number[][] = null;
	public onGStart(secenId: number, level: number, ans: JXBattleAnalysis, cb: Function): void {
		this._secenId = secenId;
		this._ans = ans;
		this._cb = cb;
		this._level = level;
		this._isWin = this._ans.isDefWin;
		if (this._level == 1) {
			GameMgr.lUserData.level = 1;
			GameMgr.lUserData.setStarByLevel(1, 3);
		}



		if (this._secenId == PART_BTL_SCENE.ARENA) {
			// this._levelRaw = GameMgr.towerData.getRaw<STowerDataRaw>(level);
			this._TowerData = GameMgr.towerData.getRaw<STowerDataRaw>(level);
		} else {
			this._levelRaw = GameMgr.storyData.getRaw<SStoryDataRaw>(level);
		}


		this.win.maskNode.opacity = 200;
		this.setView()
		this.shareBtn.active = GameMgr.sdkMgr.isShowRecordBtn();
	}

	protected setView() {
		let star = 0
		let reward: number[][] = [];
		AudioMgr.Ins().stopMusic();
		if (this._isWin) {
			AudioMgr.Ins().playEffect(Res.audio.suc);
		} else {
			AudioMgr.Ins().playEffect(Res.audio.lost);
		}
		TyqEventMgr.ins.onEndGame(this._isWin);
		switch (this._secenId) {
			case PART_BTL_SCENE.ARENA: {
				let levelRaw = this._levelRaw as STowerDataRaw;
				if (this._isWin) {
					this.sk_Over.setAnimation(0, JXEOverAniNames.shengli1, false)
					this.scheduleOnce(() => {
						this.sk_Over.setAnimation(0, JXEOverAniNames.shengli2, true)
					}, 0.75)
					this.stars.active = false;
					GameMgr.lUserData.sportsLv = this._TowerData.id;
					reward = this._TowerData.winAward;

				} else {
					this.sk_Over.setAnimation(0, JXEOverAniNames.shibai1, false)
					this.scheduleOnce(() => {
						this.sk_Over.setAnimation(0, JXEOverAniNames.shibai2, false)
					}, 0.75)
					reward = this._TowerData.lose;
				}
				this.setHeroHurt();
				break;
			}
			case PART_BTL_SCENE.GUANQIA: {

				let levelRaw = this._levelRaw as SStoryDataRaw;
				if (this._isWin) {
					let starTarget = levelRaw.win;
					starTarget.forEach(element => {
						if (this._ans.barrackHps >= element) {
							star++;
						}
					});
					reward = this._levelRaw["star" + star];
					// GameMgr.lUserData.level = levelRaw.id;
					this.sk_Over.setAnimation(0, JXEOverAniNames.shengli1, false)
					this.scheduleOnce(() => {
						this.sk_Over.setAnimation(0, JXEOverAniNames.shengli2, true)
					}, 0.75)
					GameMgr.lUserData.setStarByLevel(this._levelRaw.id, star);
					this.setHeroHurt();
				} else {
					if (this._level >= 4) {
						this.heroHurt.active = false;
						this.btnLayer.active = false;
						this.quzengqiang.active = true;
					}
					reward = this._levelRaw.lose;
					this.sk_Over.setAnimation(0, JXEOverAniNames.shibai1, false)
					this.scheduleOnce(() => {
						this.sk_Over.setAnimation(0, JXEOverAniNames.shibai2, false)
					}, 0.75)
				}

				break;
			}
		}

		this._reward = reward;
		// this.setReward(reward)



		this.setStar(star);
		// this.setHeroHead();
	}

	protected setHeroHurt() {
		var id = null;
		var num = 0;
		var heroInBattle = GameMgr.lUserData.heroInBattle;
		heroInBattle.forEach((v) => {
			let hurt = this._ans.heroHurdInfo.get(v);
			if (hurt > num) {
				num = hurt;
				id = v;
			}
		})

		this.assetImpl.prefab(Res.prefab.vw.fight.btHeroHurt, (prefab: cc.Prefab) => {
			this._ans.defTPL.partnerData.forEach((v: IHeroBtlData, k: number) => {
				let node = cc.instantiate(prefab);
				let comp = node.getComponent(BtHeroHurt);
				this.heroHurt.addChild(node);
				comp.setView(v.tableId, this._ans, id);
			})
		})
	}

	protected setStar(star: number) {
		this._star = star;
		this.stars.children.forEach((node, index: number) => {
			// star >= (index + 1) ? ShaderUtil.normal(node) : ShaderUtil.gray(node);
			star >= (index + 1) ? this.stars.children[index].getComponent(sp.Skeleton).setAnimation(0, 'start', false) : null;
		})
	}

	protected setReward(reward: number[][]) {
		this.assetImpl.prefab(Res.prefab.item.jxItem, (prefab: cc.Prefab) => {
			for (let i = 0; i < reward.length; i++) {
				let rjxItem = new RJXItem(reward[i]);
				let node = cc.instantiate(prefab);
				let comp = node.getComponent(JXItem);
				node.setScale(1.2);
				comp.setView(rjxItem, ITEM_DETAIL_FLAG.LIGHT | ITEM_DETAIL_FLAG.SHOWLIGHT);
				this.rewardLayer.addChild(node)
			}
		})
	}


	protected setHeroHead() {
		let basic = GameMgr.systemConfig.value<number>(JXDef.SYS_CONFIG_KEY.basic);
		let allExp = this._star > 0 ? this._levelRaw.exp[this._star - 1] : 0;
		let basicExp = Math.round(allExp * basic);
		let otherExp = allExp - basicExp;
		this.assetImpl.prefab(Res.prefab.vw.fight.resultHero, (prefab: cc.Prefab) => {
			this._ans.defTPL.partnerData.forEach((v: IHeroBtlData, k: number) => {
				let node = cc.instantiate(prefab);
				let comp = node.getComponent(JXResultHero);
				let hurt = this._ans.heroHurdInfo.get(v.tableId);
				hurt = hurt ? hurt : 0;
				let exp = Math.round(basicExp / this._ans.heroHurdInfo.size + otherExp * (hurt / this._ans.allHurt));
				if (!exp) {
					exp = 0;
				}
				comp.setView(v.tableId, exp)
				this.heroLayer.addChild(node);
			})
		})
	}

	protected onGetRewardClick(_, isDouble: string) {
		let doubleGet = Number(isDouble);
		if (doubleGet) {
			GameMgr.sdkMgr.watchAd(() => {
				this._reward.forEach((ele, index) => {
					this._reward[index][PART_ITEM.NUM] += ele[PART_ITEM.NUM]
				})
				GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, this._reward)
			})
		} else {
			GCtrl.ES.emit(CMsg.client.bag.onBagItemChange, this._reward)
		}
		this.onClose();
	}



	/**继续 */
	public continue() {
		if (this._secenId == PART_BTL_SCENE.GUANQIA) {
			if (this._level >= 4 && !this._isWin) {
				if (this._cb) {
					this._cb();
				}
				GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.heroPage);
				GCtrl.ES.emit(CMsg.client.home.onaddLevel);
			} else {
				super.onClose();
				GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.battleReward, this._secenId, this._level, this._ans, this._cb);
			}
		} else if (this._secenId == PART_BTL_SCENE.ARENA) {
			super.onClose();
			GameMgr.jumpToMgr.jumpGoTo(VIEW_ID.battleReward, this._secenId, this._level, this._ans, this._cb);
		}


	}
}